package nc.ui.zzjs.zzzzp;

import nc.ui.pub.beans.UIDialog;
import nc.ui.pub.bill.BillCardPanel;
import nc.ui.trade.controller.IControllerBase;
import nc.ui.zzjs.pub.BillRowNo;
import nc.ui.zzjs.pub.FlowManageEventHandler;
import nc.vo.pub.BusinessException;
import nc.vo.pub.ValidationException;
import nc.vo.querytemplate.TemplateInfo;
import nc.vo.zzjs.pub.billConst.ZZBillStatus;
import nc.vo.zzjs.pub.billConst.ZZPubTool;

public class ClientEventHandler extends FlowManageEventHandler {

	public ClientUIQueryDlg queryDialog = null;
	

	public ClientEventHandler(ClientUI clientUI, IControllerBase control) {
		super(clientUI, control);
	}

	protected UIDialog createQueryUI() {
		if (queryDialog == null) {
			TemplateInfo tempinfo = new TemplateInfo();
			tempinfo.setPk_Org(_getCorp().getPk_corp());
			tempinfo.setCurrentCorpPk(_getCorp().getPk_corp());
			tempinfo.setFunNode(getBillUI()._getModuleCode());
			tempinfo.setUserid(getBillUI()._getOperator());
			queryDialog = new ClientUIQueryDlg(getBillUI(), null, tempinfo);
		}
		return queryDialog;
	}
	
	@Override
	protected String getHeadCondition() {
		return " zz_zzp_h.pk_corp = '"+_getCorp().getPrimaryKey()+"'  and  zz_zzp_h.pk_billtype = '"+ZZBillStatus.zz09+"'";	}
	
	public BillCardPanel getBillCardPanel() {
		return getBillCardPanelWrapper().getBillCardPanel();
	}
	
	
	@Override
	protected ClientUI getBillManageUI() {
		return (ClientUI) getBillUI();
	}
	
	@Override
	protected void onBoLineAdd() throws Exception {
		// TODO Auto-generated method stub
		super.onBoLineAdd();
		BillRowNo.addLineRowNo(getBillCardPanelWrapper().getBillCardPanel(),ZZBillStatus.zz09, "crowno");
		int row = getBillCardPanel().getBillTable().getSelectedRow();
		getBillCardPanel().getBillModel().setValueAt("1003",row,"csendcorpid");
		String csendcalid = ZZPubTool.getString_TrimZeroLenAsNull(getBillCardPanel().getHeadItem("pk_calbody").getValueObject());
		getBillCardPanel().getBillModel().setValueAt(csendcalid,row,"csendcalid");
		getBillCardPanel().getBillModel().execLoadFormula();;
	}
	
	@Override
	protected void onBoEdit() throws Exception {
		// TODO Auto-generated method stub
		super.onBoEdit();
		String ibusinessobj = ZZPubTool.getString_TrimZeroLenAsNull(getBillCardPanel().getHeadItem("ibusinessobj").getValueObject());
		if("1".equalsIgnoreCase(ibusinessobj))
			setHeadItemEnable(true);
		else if("0".equalsIgnoreCase(ibusinessobj))
			setHeadItemEnable(false);
	}
	
	private void setHeadItemEnable(boolean flag){
		getBillCardPanel().getHeadItem("pk_member").setEnabled(flag);
		getBillCardPanel().getHeadItem("pk_cumandoc").setEnabled(flag);
		getBillCardPanel().getHeadItem("pk_manager").setEnabled(!flag);
		//getBillCardPanel().getHeadItem("crareaid").setEnabled(!flag);
	}
	
	@Override
	protected void onBoCommit() throws Exception {
        try {
            dataNotNullValidate();
        } catch (ValidationException e) {
            throw new BusinessException(e.getMessage());
        }
        beforeSaveCheck();
        super.onBoCommit();
    }
	
}
